package com.yanyilu.qiubite.controller;

import com.yanyilu.qiubite.entity.AdminUser;
import com.yanyilu.qiubite.entity.Gift;
import com.yanyilu.qiubite.jwt.Audience;
import com.yanyilu.qiubite.service.AdminUserService;
import com.yanyilu.qiubite.service.GiftService;
import com.yanyilu.qiubite.util.PageUtil;
import com.yanyilu.qiubite.util.ParseJwtUtil;
import com.yanyilu.qiubite.util.R;
import com.yanyilu.qiubite.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("gift")
public class GiftController {
    @Autowired
    GiftService giftService;

    @Autowired
    Audience audience;

    @Autowired
    AdminUserService adminUserService;

    /**
     * 奖品列表
     * @param pageNum
     * @param pageSize
     * @param giftName
     * @return
     */
    @RequestMapping("/admin/list")
    public R list(@RequestParam("page") Integer pageNum,
                  @RequestParam("limit") Integer pageSize,
                  @RequestParam(value = "giftName",required = false)String giftName,
                  @RequestParam(value = "spotName",required = false)String spotName){

        String role = ParseJwtUtil.getRole(audience);
        Integer adminUserId = ParseJwtUtil.getUserId();

        if("1".equals(role)){

            /************************************************************/
            Pageable pagesable = PageUtil.pagelimit(pageNum, pageSize);
            AdminUser adminUser = adminUserService.findUserById(adminUserId);

            Gift gift = new Gift();

            gift.setSpotId(adminUser.getSpotId());
            if(null != giftName){
                gift.setGiftName(giftName);
            };

            ExampleMatcher matcher = ExampleMatcher.matching()
                    .withMatcher("giftName", ExampleMatcher.GenericPropertyMatchers.contains());

            Example<Gift> example = Example.of(gift,matcher);
            List<Gift> giftList = giftService.spotGiftList(example,pagesable);
            return R.ok().put("list",giftList);
            /****************************************************************/

        }else if("0".equals(role)){

            Pageable pagesable = PageUtil.pagelimit(pageNum, pageSize);

            Gift gift = new Gift();

            if(null != giftName){
                gift.setGiftName(giftName);
            };
            if(null != spotName){
                gift.setSpotName(spotName);
            };

            ExampleMatcher matcher = ExampleMatcher.matching()
                    .withMatcher("giftName", ExampleMatcher.GenericPropertyMatchers.contains())
                    .withMatcher("spotName", ExampleMatcher.GenericPropertyMatchers.contains());

            Example<Gift> example = Example.of(gift,matcher);
            List<Gift> giftList = giftService.spotGiftList(example,pagesable);
            return R.ok().put("list",giftList);

        } else {
            return R.ok(ResultCode.NO_ROLE_ERROR);
        }
    }

    /**
     * 添加礼物/修改礼物
     * @param gift
     * @return
     */
    @RequestMapping("/admin/add")
    public R add(@RequestBody Gift gift){

        giftService.giftSave(gift);
        return R.ok();
    }

    /**
     * 删除礼物
     * @param giftId
     * @return
     */
    @RequestMapping("/admin/delete")
    public R delete(@RequestParam("giftId")Integer giftId){

        giftService.giftDeleteById(giftId);
        return R.ok();
    }
}
